//package com.diver.flinkdemo.utils;
//
//import com.alibaba.druid.pool.DruidDataSource;
//import com.baomidou.mybatisplus.annotation.DbType;
//import com.baomidou.mybatisplus.core.MybatisConfiguration;
//import com.baomidou.mybatisplus.core.config.GlobalConfig;
//import com.baomidou.mybatisplus.core.incrementer.DefaultIdentifierGenerator;
//import com.baomidou.mybatisplus.core.injector.DefaultSqlInjector;
//import com.baomidou.mybatisplus.core.mapper.BaseMapper;
//import com.baomidou.mybatisplus.core.toolkit.GlobalConfigUtils;
//import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
//import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
//import com.zaxxer.hikari.HikariDataSource;
//import lombok.extern.slf4j.Slf4j;
//import org.apache.ibatis.builder.xml.XMLMapperBuilder;
//import org.apache.ibatis.logging.slf4j.Slf4jImpl;
//import org.apache.ibatis.mapping.Environment;
//import org.apache.ibatis.plugin.Interceptor;
//import org.apache.ibatis.session.SqlSession;
//import org.apache.ibatis.session.SqlSessionFactory;
//import org.apache.ibatis.session.SqlSessionFactoryBuilder;
//import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory;
//
//import javax.sql.DataSource;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.io.InputStream;
//import java.net.JarURLConnection;
//import java.net.URL;
//import java.util.Enumeration;
//import java.util.Properties;
//import java.util.jar.JarEntry;
//import java.util.jar.JarFile;
//
///**
// * @author LuJW
// * @date 2023/3/31 9:50
// */
//@Slf4j
//public class DataSourceFactoryUtil {
//    //mysql sessionFactory
//    public static SqlSessionFactory mysqlSessionFactory;
//    //doris sessionFactory
////    public static SqlSessionFactory dorisSessionFactory;
//    //mysql 配置文件路径
//    private final String PropertiesFilePath = "/DataSource.properties";
//    //doris 配置文件路径
////    private String dorisPropertiesFilePath = "dorisDataSource.properties";
//    //mysql 文件inputStream
//    private InputStream mysqlInputStream;
//    //doris 文件inputStream
//    private InputStream dorisInputStream;
//    //mapper类所在路径
//    private String mapperPackagePath ;
//
//    private Properties properties;
//
////    @PostConstruct
//    public void init() throws IOException {
//        log.info("mybatis builder 初始化...");
//        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
//        //这是mybatis-plus的配置对象，对mybatis的Configuration进行增强
//        MybatisConfiguration configuration = new MybatisConfiguration();
//        //这是初始化配置，后面会添加这部分代码
//        initConfiguration(configuration);
//        //这是初始化连接器，如mybatis-plus的分页插件
//        configuration.addInterceptor(initInterceptor());
//        //配置日志实现
//        configuration.setLogImpl(Slf4jImpl.class);
//        //扫描mapper接口所在包
//        configuration.addMappers("org.jeecg.mapper");
//        //构建mybatis-plus需要的globalconfig
//        GlobalConfig globalConfig = new GlobalConfig();
//        //此参数会自动生成实现baseMapper的基础方法映射
//        globalConfig.setSqlInjector(new DefaultSqlInjector());
//        //设置id生成器
////        globalConfig.setIdentifierGenerator(new DefaultIdentifierGenerator());
//        GlobalConfigUtils.getGlobalConfig(configuration).setIdentifierGenerator(new DefaultIdentifierGenerator());
//        //设置超类mapper
//        globalConfig.setSuperMapperClass(BaseMapper.class);
//        //给configuration注入GlobalConfig里面的配置
//        GlobalConfigUtils.setGlobalConfig(configuration, globalConfig);
//
//        //mysql sqlSessionFactory创建
//        initMysqlSessionFactory(configuration, builder);
//        log.info("mysqlSessionFactory1->{}",mysqlSessionFactory);
//        //doris sqlSessionFactory创建
////        initDorisSessionFactory(configuration, builder);
//
//        //创建session
////        this.session = sqlSessionFactory.openSession();
//    }
//
//    private void initMysqlSessionFactory(MybatisConfiguration configuration, SqlSessionFactoryBuilder builder) throws IOException {
//        //设置数据源
//        configuration.addMappers("org.jeecg.mapper.mysqlMapper");
////        Environment environment = new Environment("1", new JdbcTransactionFactory(), initMysqlDataSource());
//        Environment environment = new Environment("1", new JdbcTransactionFactory(), initMysqlByDruidDataSource());
//        configuration.setEnvironment(environment);
//
//        this.registryMapperXml(configuration, "org.jeecg.mapper/mysqlMapper");
//        //构建sqlSessionFactory
//        mysqlSessionFactory = builder.build(configuration);
//    }
//
//    private void initDorisSessionFactory(MybatisConfiguration configuration, SqlSessionFactoryBuilder builder) throws IOException {
//        //设置数据源
//        configuration.addMappers("org.jeecg.mapper.dorisMapper");
//        Environment environment = new Environment("2", new JdbcTransactionFactory(), initDorisDataSource());
//        configuration.setEnvironment(environment);
//
//        this.registryMapperXml(configuration, "org.jeecg.mapper/dorisMapper");
//        //构建sqlSessionFactory
////        dorisSessionFactory = builder.build(configuration);
//    }
//
//
//    /**
//     * 初始化配置
//     *
//     * @param configuration
//     */
//    private void initConfiguration(MybatisConfiguration configuration) {
//        //开启驼峰大小写转换
//        configuration.setMapUnderscoreToCamelCase(true);
//        //配置添加数据自动返回数据主键
//        configuration.setUseGeneratedKeys(true);
//    }
//
//    /**
//     * 初始化拦截器
//     *
//     * @return
//     */
//    private Interceptor initInterceptor() {
//        //创建mybatis-plus插件对象
//        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//        //构建分页插件
//        PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//        paginationInnerInterceptor.setDbType(DbType.MYSQL);
//        paginationInnerInterceptor.setOverflow(true);
//        paginationInnerInterceptor.setMaxLimit(500L);
//        interceptor.addInnerInterceptor(paginationInnerInterceptor);
//        return interceptor;
//    }
//
//    /**
//     * 初始化数据源
//     *
//     * @return
//     */
//    private DataSource initMysqlDataSource() throws IOException {
//        log.info("PropertiesFilePath->{}",PropertiesFilePath);
//        mysqlInputStream = this.getClass().getResourceAsStream(PropertiesFilePath);
//        log.info("inputStream->{}",mysqlInputStream);
//        properties = new Properties();
//        properties.load(mysqlInputStream);
//        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setJdbcUrl(properties.getProperty("mysql.url"));
//        dataSource.setDriverClassName(properties.getProperty("driverClassName"));
//        dataSource.setUsername(properties.getProperty("mysql.username"));
//        dataSource.setPassword(properties.getProperty("mysql.password"));
//        return otherConfig(dataSource);
//    }
//    /**
//     * 初始化数据源
//     *
//     * @return
//     */
//    private DataSource initMysqlByDruidDataSource() throws IOException {
//        log.info("PropertiesFilePath->{}",PropertiesFilePath);
//        mysqlInputStream = this.getClass().getResourceAsStream(PropertiesFilePath);
//        log.info("inputStream->{}",mysqlInputStream);
//        properties = new Properties();
//        properties.load(mysqlInputStream);
//        DruidDataSource dataSource = new DruidDataSource();
//        dataSource.setUrl(properties.getProperty("mysql.url"));
////        HikariDataSource dataSource = new HikariDataSource();
////        dataSource.setJdbcUrl(properties.getProperty("mysql.url"));
//        dataSource.setDriverClassName(properties.getProperty("driverClassName"));
//        dataSource.setUsername(properties.getProperty("mysql.username"));
//        dataSource.setPassword(properties.getProperty("mysql.password"));
//        return otherConfigForDruid(dataSource);
//    }
//
//    /**
//     * 初始化数据源
//     *
//     * @return
//     */
//    private DataSource initDorisDataSource() throws IOException {
//        dorisInputStream = this.getClass().getResourceAsStream(PropertiesFilePath);
//        Properties properties = new Properties();
//        properties.load(dorisInputStream);
//        HikariDataSource dataSource = new HikariDataSource();
//        dataSource.setJdbcUrl(properties.getProperty("doris.url"));
//        dataSource.setDriverClassName(properties.getProperty("driverClassName"));
//        dataSource.setUsername(properties.getProperty("doris.username"));
//        dataSource.setPassword(properties.getProperty("doris.password"));
//        return otherConfig(dataSource);
//    }
//
//    private HikariDataSource otherConfig(HikariDataSource dataSource) {
//        dataSource.setIdleTimeout(Integer.parseInt(properties.getProperty("idleTimeout")));
//        boolean autoCommit;
//        autoCommit = "true".equals(properties.getProperty("autoCommit"));
//        dataSource.setAutoCommit(autoCommit);
//        dataSource.setMaximumPoolSize(Integer.parseInt(properties.getProperty("maximumPoolSize")));
//        dataSource.setMinimumIdle(Integer.parseInt(properties.getProperty("minimumIdle")));
//        dataSource.setMaxLifetime(Integer.parseInt(properties.getProperty("maxLifetime")));
//        dataSource.setConnectionTestQuery("SELECT 1");
//        dataSource.setConnectionTimeout(Integer.parseInt(properties.getProperty("connectionTimeout")));
//        return dataSource;
//    }
//
//    private DruidDataSource otherConfigForDruid(DruidDataSource dataSource) {
////        dataSource.settimeout(Integer.parseInt(properties.getProperty("idleTimeout")));
//        boolean autoCommit;
//        autoCommit = "true".equals(properties.getProperty("autoCommit"));
//        dataSource.setDefaultAutoCommit(autoCommit);
//        dataSource.setValidationQuery("SELECT 1");
//        dataSource.setRemoveAbandoned(true);
//        dataSource.setRemoveAbandonedTimeout(60);
//        dataSource.setInitialSize(10);
//        dataSource.setMaxActive(Integer.parseInt(properties.getProperty("maximumPoolSize")));
//        dataSource.setMinIdle(10);
//        dataSource.setMaxWait(Integer.parseInt(properties.getProperty("maxLifetime")));
//        dataSource.setMinEvictableIdleTimeMillis(60000);
//        dataSource.setTimeBetweenEvictionRunsMillis(30000);
//        dataSource.setTestWhileIdle(true);
//        return dataSource;
//    }
//
//    /**
//     * 解析mapper.xml文件
//     * @param configuration
//     * @param classPath
//     * @throws IOException
//     */
//    private void registryMapperXml(MybatisConfiguration configuration, String classPath) throws IOException {
//        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
//        Enumeration<URL> mapper = contextClassLoader.getResources(classPath);
//        while (mapper.hasMoreElements()) {
//            URL url = mapper.nextElement();
//            if (url.getProtocol().equals("file")) {
//                String path = url.getPath();
//                File file = new File(path);
//                File[] files = file.listFiles();
//                for (File f : files) {
//                    FileInputStream in = new FileInputStream(f);
//                    XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, f.getPath(), configuration.getSqlFragments());
//                    xmlMapperBuilder.parse();
//                    in.close();
//                }
//            } else {
//                JarURLConnection urlConnection = (JarURLConnection) url.openConnection();
//                JarFile jarFile = urlConnection.getJarFile();
//                Enumeration<JarEntry> entries = jarFile.entries();
//                while (entries.hasMoreElements()) {
//                    JarEntry jarEntry = entries.nextElement();
//                    if (jarEntry.getName().endsWith(".xml")) {
//                        InputStream in = jarFile.getInputStream(jarEntry);
//                        XMLMapperBuilder xmlMapperBuilder = new XMLMapperBuilder(in, configuration, jarEntry.getName(), configuration.getSqlFragments());
//                        xmlMapperBuilder.parse();
//                        in.close();
//                    }
//                }
//            }
//        }
//    }
//
//    public static void sessionDoAfter(SqlSession session) {
////        session.commit();
//        session.close();
//    }
//}
